const host = {
  Dev: "http://test.com"
}

const api = {
  getUserInfo: "...",
  getGoods: "..."
}

export default function ({ url, data, showLoading = false }) {
  let self = this;
  changeFetchState(self, true);
  showLoading && wx.showLoading({ title: showLoading })
  return new Promise((resolve, reject) => {
    // demo 实际使用中注掉 ⤵️
    return resolve([{ id: 123456 }]);
    // ⬆️
    const options = {
      url: host.Dev + api[url],
      method: "POST",
      header: { "content-type": "application/json" },
      data,
      success: ({ data }) => {
        resolve(data.data, data);
      },
      fail: err => {
        reject(err);
      },
      complete() {
        changeFetchState(self, false);
        showLoading && wx.hideLoading();
      }
    };

    wx.request(options);
  })
}
// 以当前作用域调用，可控制页面需要请求状态的组件
function changeFetchState(self, state) {
  self && self.setData({ _fetchState: state });
}